House setting: Basic Rules

Zulquar Nain
AMU

February 10, 2024

Working Directory

Working Directory

  • The working directory in R is the folder where we are working.

  • Store files or load them

  • R Objects are saved

Know the Working Directory

To check the directory of a R session, use the function getwd()

The Code
#Find the path of the working directory
getwd()
[1] "E:/pen/New folder (2)/Rclass_2024"

Setting the Working Directory

setwd() function

To change the working directory, we use setwd() function

We must specify the path of new working directory folder

The Code
#To set new the working directory

 setwd ("E:/pen/New folder (2)/Rclass_2024")

RStudio-GUI

Change working directory in RStudio

  • In order to create a new directory go to Session \(\rightarrow\) Set Working Directory

  • Select the option there

Your TURN

  • Check the working directory

  • Change the working directory

Projects

Projects

  • Projects allow you to keep a collection of files all together, including:

    • R scripts

    • RMarkdown files (more on this later)

    • Data files

    • And much more!

  • very useful to organize our scripts in folders

  • when opening a project, it will contain all the files corresponding to it. Also, the project folder will be set as the working directory

  • sets the working directory and makes relative path

Creating a Project

Files in R

Files in R

File Types

. . .

There are two main file types that you’ll work with

. . .

R scripts (.R)

Text is assumed to be executable R code unless you comment it (more on this soon)

The Code
# This is a comment

data <- read_csv("train.csv")

RMarkdown files (.Rmd)

Text is assumed to be text unless you put it in a code chunk (more on this Later)

R script?

  • While you can run/execute r code using R console with ease.

  • It is time consuming.

  • Each time you have to re-enter a command to execute it.

  • Be calm, we have solution that is R scripts

  • A script is simply a text file containing a set of commands and comments. The script can be saved and used later to re-execute the saved commands. The script can also be edited so you can execute a modified version of the commands.

Creating R Script

Create new script file: File -> New File -> R Script

R Script

R Script

How to execute the Code

  • To execute the code: control + enter on Windows, command + enter on Mac keystrokes or use Run button

R Script

  • Note that you don’t have to highlight code. You can just hit run anywhere on line to run code.

Comments

  • Do them for others, and for your future self.
# This is a comment

#head(data, n = 5)

Packages

Packages

  • What is R package?

  • A collection of code related to specific tasks

  • As of Today, there are 20429 Packages

  • Packages relevant to specific area can be viewed at CRANTASKVIEW

  • How to use a package?

    • Install a package using install.packages("package name")
    • Loading/attaching a package in current session use library(package name)

Packages

  • data.table
  • readxl

Your TURN

  • Create a Project

  • Create a script file

  • Install TWO packages using script file

Have patience